#include <avr/io.h>
#include <avr/interrupt.h>  /* for sei() */
#include <util/delay.h>     /* for _delay_ms() */
#include <avr/pgmspace.h>   /* required by usbdrv.h */
#include "usbdrv.h"

#define VENDOR_RQ_SET_LED    0
<<<<<<< .mine
#define VENDOR_RQ_GET_IN  1
=======
#define VENDOR_RQ_GET_SWITCH 1
>>>>>>> .r28

/* ------------------------------------------------------------------------- */
/* ---------------------------- Utility Functions -------------------------- */
/* ------------------------------------------------------------------------- */

char in_c(char pin)
{
    // read logic level of port-C's pin
    DDRC &= ~(1<<pin);
    return ((PINC & (1<<pin))>>pin);    
} 

void out_c(char pin, char val)
{
    // write logic level to port-C's pin
    DDRC |= 1<<pin;
    if(val)
        PORTC |= 1<<pin;
    else
        PORTC &= ~(1<<pin);
}

/* ------------------------------------------------------------------------- */
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */

usbMsgLen_t usbFunctionSetup(uchar data[8])
{
    usbRequest_t *rq = (void *)data;
    static uchar dataBuffer[1];  /* must stay valid when usbFunctionSetup returns */

    if (rq->bRequest == VENDOR_RQ_SET_LED)
    {
        uint8_t led_no  = rq->wValue.bytes[0];
        uint8_t led_val = rq->wValue.bytes[1];

		uint8_t pin_0 ,pin_2,pin_4;
		pin_0 = led_no%2;
		led_no = led_no/2;
		pin_2 = led_no%2;
		led_no = led_no/2;
		pin_4 = led_no%2;

        out_c(0, pin_0*led_val);
        out_c(2, pin_2*led_val);
        out_c(4, pin_4*led_val);
        return 0;
    }
    else if (rq->bRequest == VENDOR_RQ_GET_IN)
    {
        dataBuffer[0] = (!in_c(1)*4)+(!in_c(3)*2)+!in_c(5);
        usbMsgPtr = dataBuffer;
        return 1;
<<<<<<< .mine
    } 
/* 	else if (rq->bRequest == VENDOR_RQ_GET_LIGHT)
	{
		uint16_t x = read_adc(4);
		dataBuffer[0] = x/256;
		dataBuffer[1] = x%256;
		usbMsgPtr = dataBuffer;
		return 2;
	}*/
=======
    }
>>>>>>> .r28
    return 0;   /* default for not implemented requests: return no data back to host */
}

/* ------------------------------------------------------------------------- */

int main(void)
{
    DDRC  = 0b00101010;  // PC0..PC2 = Output, PC3..PC5 = Input
    PORTC = 0b00000000;  // PC7..PC0 = 0
  //  PORTC |= (1 << 3);   // Enable pull-up at PC3
    DDRD  = 0b00001000;
    PORTD = 0b00000000;

    usbInit();
    usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
    _delay_ms(300);
    usbDeviceConnect();
    sei();
    for(;;){                /* main event loop */
        usbPoll();
    }
    return 0;
} /* ------------------------------------------------------------------------- */
